//
//  SqList.cpp
//  DS_Demo
//
//  Created by apple on 2023/9/28.
//

#include "SqList.hpp"
#include <iostream>
Status InitList(SqList *L){
    for(int i=0; i<MAXSIZE; i++)
        L->data[i] = -1;
    L->length = 0;
    return OK;
}

Status DestroyList(SqList *L){
    for(int i = 0; i < MAXSIZE; i++ )
        L->data[i] = -1;
    L->length = 0;
    return OK;
}

void ClearList(SqList *L){
    for(int i=0; i < L->length; i++)
    {
        L->data[i] = -1;
        L->length--;
    }
    
}

Status ListEmpty(SqList L){
    if(L.length <= 0)
        return TRUE;
    return FALSE;
    
}

int ListLength(SqList L){ return L.length-1; }

Status GetElem(SqList L, int i, LElemType *e){
    if(i > L.length) {
        std::cerr << "所给索引i:"<<i<<"超过当前线性表的长度。"<<std::endl;
        return FALSE;
    }
    *e = L.data[i];
    return TRUE;
}

Status LocateElem(SqList L, LElemType e){
    for(int i = 0; i < L.length; i++)
        if(e == L.data[i])
            return i;
    return FALSE;
}

Status PriorElem(SqList L, LElemType cur_e, LElemType *pre_e){
    int pre_index = 0;
    for(int i = 0; i< L.length; i++){
        pre_index = i;
        if(L.data[i] == cur_e)
        {
            pre_index = i-1;
            break;
        }
    }
    if(pre_index == -1 || pre_index >= L.length) return ERROR;
    *pre_e = L.data[pre_index];
    return OK;
}

Status NextElem(SqList L, LElemType cur_e, LElemType *next_e){
    int post_index = 0;
    for(int i = 0; i < L.length; i++){
        post_index = i;
        if(L.data[i] == cur_e){
            post_index = i+1;
            break;
        }
        
    }
    if(post_index >= L.length) return ERROR;
    *next_e = L.data[post_index];
    return OK;
}

Status ListInsert(SqList *L, int i, LElemType e){
    if(i<0||i>L->length) return NOTOK;
    if(L->length>MAXSIZE) return OVERFLOW;
    for(int k = L->length-1; k>=i; --k)
        L->data[k+1]=L->data[k];
    L->data[i] = e;
    L->length++;
    return OK;
}

Status ListDelete(SqList *L, int i, LElemType *e){
    if(ListEmpty(*L)) return ERROR;
    if(i<0||i>L->length) return ERROR;
    *e = L->data[i];
    for(int k = i; k < L->length; ++k)
        L->data[k] = L->data[k+1];
    L->data[L->length-1] = -1;
    L->length--;
    return OK;
}

void ListTraverse(SqList L){
    for(int i = 0; i < L.length; i++)
        std::cout << L.data[i] <<" ";
    std::cout <<std::endl;
}

/* 将线性表Lb中的数据插入到La中*/
void UnionList(SqList *La, SqList Lb){
    int La_len = ListLength(*La);
    int Lb_len = ListLength(Lb);
    LElemType* cur_e = 0;
    for(int i=0; i < Lb_len; ++i){
        GetElem(Lb, i, cur_e);
        if(!LocateElem(*La, *cur_e))
            ListInsert(La,++La_len, *cur_e);
    }
}

void MergeList(SqList La, SqList Lb, SqList *Lc){
    InitList(Lc);
    int i=0,j=0,k=0;
    int La_len = ListLength(La);
    int Lb_len = ListLength(Lb);
    LElemType *ai=0, *bj=0;
    while(i<=La_len&&j<=Lb_len){
        GetElem(La, i, ai);
        GetElem(Lb, j, bj);
        if(*ai <= *bj){
            ListInsert(Lc, ++k, *ai);
            ++i;
        }
        else{
            ListInsert(Lc, ++k, *bj);
            ++j;
        }
    }
    while(i<=La_len){
        GetElem(La, i++, ai);
        ListInsert(Lc, ++k, *ai);
    }
    while(j<=Lb_len){
        GetElem(Lb, j++, bj);
        ListInsert(Lc, ++k, *bj);
    }
    
    
}

void IncSort(SqList *L){
    int i,j,temp;
    for(i=1; i<L->length; ++i){
        if(L->data[i]<L->data[i-1]){
            temp = L->data[i];
            for(j=i-1; temp<L->data[j]; --j)
                L->data[j+1] = L->data[j];
            L->data[j+1] = temp;
        }
    }
}

void DecSort(SqList *L){
    int i,j,temp;
    for(i=1; i<L->length; ++i){
        if(L->data[i]>L->data[i-1]){
            temp = L->data[i];
            for(j=i-1; temp>L->data[j]; --j)
                L->data[j+1] = L->data[j];
            L->data[j+1] = temp;
        }
    }
    
}

